<template>
	<view class="content">
		<view class="header"> </view>
		<view class="money-box">
			<view class="money-box__li">
				<view class="price">{{ countArr.hasUseMoney }}</view>
				<view>已到账(元）</view>
			</view>
			<view class="money-box__li">
				<view class="price">{{ countArr.useingMoney }}</view>
				<view>入帐中(元）</view>
			</view>
			<view class="money-box__li">
				<view class="price">{{ countArr.unUseMoney }}</view>
				<view>未使用(元）</view>
			</view>
		</view>
		<view class="tab-box">
			<block v-for="(itemT, indexT) in tabArr" :key="indexT">
				<view
					class="tab-box__li"
					:class="params.recordStatus == itemT.type ? 'active' : ''"
					@tap="changeTab(itemT.type)"
				>
					<view :class="params.recordStatus == itemT.type ? 'li-line' : 'line'">{{
						itemT.value
					}}</view>
				</view>
			</block>
		</view>
		<view class="hb-list">
			<view v-if="list.length > 0">
				<block v-for="(item, index) in list" :key="index">
					<view
						class="hb-list__li"
						:class="[
							{
								active: item.recordStatus == 1
							},
							{
								noActive: item.recordStatus == 2
							}
						]"
					>
						<view class="hb-list__li-lift">
							<view class="hb-list__li-img">
								<image
									v-if="item.recordStatus == 2"
									src="@/static/images/integral/honhbao2.png"
								></image>
								<image v-else src="@/static/images/integral/honhbao.png"></image>
								<view class="money">￥{{ item.money | unitMoney }}</view>
							</view>
							<view class="hb-list__li-value">
								<view
									class="value-p"
									:class="[
										{
											activeY: item.recordStatus == 1
										},
										{
											activeN: item.recordStatus == 2
										}
									]"
								>
									￥<text>{{ item.money }} </text></view
								>
								<view
									class="value-v"
									:class="[
										{
											activeY: item.recordStatus == 1
										},
										{
											activeN: item.recordStatus == 2
										}
									]"
								>
									{{ item.remark }}
								</view>
								<view class="value-t">
									使用期限:{{ item.startTime | dateFormatSlash }} -
									{{ item.endTime | dateFormatSlash }}
								</view>
							</view>
						</view>
						<view class="hb-list__li-btn" v-if="item.recordStatus == 0" @tap="toPay">
							去使用
						</view>
						<view class="hb-list__li-icon" v-if="item.recordStatus == 1">
							<image src="@/static/images/integral/icon-used.svg"></image>
						</view>
						<view class="hb-list__li-icon" v-if="item.recordStatus == 2">
							<image src="@/static/images/integral/icon-expired.svg"></image>
						</view>
					</view>
				</block>
				<LoadMore :finished="isReachBottom"></LoadMore>
			</view>
			<LoadMore v-else :empty="true" emptyText="暂无积分红包"></LoadMore>
		</view>
		<!--智能搜索-->
		<ai-search ref="aiSearch"></ai-search>
	</view>
</template>

<script>
import PropBox from '@/components/PropBox';
import LoadMore from '@/components/LoadMore';
import { mapGetters } from 'vuex';

export default {
	data() {
		return {
			countArr: {
				hasUseMoney: 0,
				useingMoney: 0,
				unUseMoney: 0
			},
			tabArr: [
				{
					value: '未使用',
					type: 0
				},
				{
					value: '已使用',
					type: 1
				},
				{
					value: '已失效',
					type: 2
				}
			],
			params: {
				page: 1,
				size: 15,
				recordStatus: 0
			},
			list: [],
			pagination: {},
			isReachBottom: false
		};
	},
	components: {
		PropBox,
		LoadMore
	},
	computed: {
		...mapGetters(['userInfo'])
	},
	mounted() {
		this.getList({ ...this.params });
		this.getCount();
	},
	onShow() {
		this.$nextTick(() => {
			uni.getClipboardData({
				success: res => {
					if (res.data && this.$store.state.common.aiSearch) {
						this.$refs.aiSearch.open(res.data);
						this.$store.commit({
							type: 'setAiSearch',
							aiSearch: false
						});
					}
				}
			});
		});
	},
	onHide() {
		this.$refs.aiSearch.close();
	},

	methods: {
		changeTab(e) {
			this.params.recordStatus = e;
			this.getList({ ...this.params });
		},
		getCount() {
			this.$service.integral.money
				.count()
				.then(res => {
					this.countArr = Object.assign(this.countArr, res);
				})
				.catch(err => {
					uni.showToast({
						title: err.message,
						icon: 'none'
					});
				});
		},
		getList(params) {
			this.$service.integral.money
				.changeList(params)
				.then(res => {
					this.pagination = res.pagination;
					if (res.pagination.page > 1) {
						this.list = this.list.concat(res.list);
					} else {
						this.list = res.list;
					}
					uni.stopPullDownRefresh();
				})
				.catch(err => {
					uni.stopPullDownRefresh();
					uni.showToast({
						title: err.message,
						icon: 'none'
					});
				});
		},
		toPay() {
			this.$Router.pushTab('/pages/index/index');
		}
	},
	onPullDownRefresh() {
		this.getList({ ...this.params });
		this.getCount();
	},
	onReachBottom() {
		if (
			this.pagination.page * (this.pagination.pageSize || this.pagination.size) >=
			this.pagination.total
		) {
			this.isReachBottom = true;
		} else {
			this.isReachBottom = false;
			this.getList({
				...this.params,
				page: this.pagination.page + 1
			});
		}
	}
};
</script>
<style lang="scss" scoped>
.content {
	background: #f6f7fa;
	.header {
		height: 128rpx;
		background: #f70114;
	}
	.money-box {
		width: 710rpx;
		height: 164rpx;
		margin: auto;
		border-radius: 16rpx;
		box-shadow: 0rpx 6rpx 12rpx rgba(0, 0, 0, 0.16);
		background: #ffffff;
		margin-top: -100rpx;
		@include flexBox(space-around, center);
		&__li {
			text-align: center;
			border-right: 1px solid #e8e8e8;
			flex: 1;
			color: #717076;
			font-size: 24rpx;

			.price {
				color: #f70114;
				font-size: 36rpx;
			}
		}
		&__li:last-child {
			border-right: none;
		}
	}
	.tab-box {
		@include flexBox(space-around, center);
		font-size: 28rpx;
		margin-top: 26rpx;
		&__li {
			color: #171717;
			&.active {
				color: #f70114;
			}
			.li-line {
				padding-bottom: 8rpx;
				border-bottom: 3px solid #f70114;
			}
			.line {
				padding-bottom: 8rpx;
				border-bottom: 3px solid transparent;
			}
		}
	}
	.hb-list {
		&__li {
			width: 710rpx;
			height: 186rpx;
			margin: auto;
			margin-top: 20rpx;
			border-radius: 16rpx;
			background: linear-gradient(90deg, rgba(250, 78, 73, 1) 0%, rgba(254, 0, 87, 1) 100%);
			color: #eeffaf;
			padding: 30rpx 32rpx;
			@include flexBox(space-between, center);
			&.active {
				background: #ffe7e6;
				color: #a3999c;
			}
			&.noActive {
				background: #e6e6e6;
				color: #888888;
			}
			&-lift {
				@include flexBox(center, center);
			}
			&-img {
				width: 120rpx;
				height: 126rpx;
				position: relative;
				text-align: center;
				image {
					width: 100%;
					height: 100%;
					position: absolute;
					top: 0;
					left: 0;
				}
				.money {
					font-size: 34rpx;
					color: #ff0043;
					position: relative;
					z-index: 20;
					margin-top: 12rpx;
				}
			}
			&-value {
				margin-left: 26rpx;
				.value-p {
					color: #eeffaf;
					font-size: 34rpx;
					text {
						font-size: 40rpx;
					}
					&.activeY {
						color: #ff215a;
					}
					&.activeN {
						color: #888888;
					}
				}
				.value-v {
					.value-p {
						color: #eeffaf;
						font-size: 34rpx;
						text {
							font-size: 40rpx;
						}
						&.activeY {
							color: #a3999c;
						}
						&.activeN {
							color: #888888;
						}
					}
				}
			}
			&-btn {
				width: 130rpx;
				height: 56rpx;
				background: #ffffff;
				border-radius: 34rpx;
				text-align: center;
				line-height: 56rpx;
				color: #fa4e49;
				font-size: 26rpx;
			}
			&-icon {
				width: 96rpx;
				height: 96rpx;
				image {
					width: 100%;
					height: 100%;
				}
			}
		}
	}
}
</style>
